package com.TaskOrganizer.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import com.TaskOrganizer.managers.ActionManager;
import com.TaskOrganizer.task.Task;

public class AppCore {
	private static AppCore instance;
	private ActionManager actionManager;
	private ArrayList<Task> taskList;
	private String storePath;
	public static boolean develop;

	private AppCore() {
		if (develop) {
			storePath = "";
		} else {
			JFileChooser jfc = new JFileChooser();
			jfc.getCurrentDirectory().getAbsolutePath();
			storePath = jfc.getCurrentDirectory().getAbsolutePath() + "\\";
		}
		actionManager = new ActionManager();
		taskList = new ArrayList<Task>();
		openTaskList();
	}

	public static AppCore getInstance() {
		if (instance == null) {
			instance = new AppCore();
		}
		return instance;
	}

	public ActionManager getActionManager() {
		return actionManager;
	}

	public void removeTask(Task task) {
		taskList.remove(task);
		saveTaskList();
	}

	public void addTask(Task task) {
		taskList.add(task);
		saveTaskList();
	}

	@SuppressWarnings("unchecked")
	private void openTaskList() {
		File file = new File(storePath + "TaskOrganizer\\data\\tasks.tsk");
		if (!file.exists())
			return;
		try {
			ObjectInputStream inputStream = new ObjectInputStream(
					new FileInputStream(file.getAbsolutePath()));
			taskList = (ArrayList<Task>) inputStream.readObject();
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void saveTaskList() {
		ObjectOutputStream fos;
		File tmp = new File(storePath + "TaskOrganizer\\data");
		if (!tmp.exists()) {
			tmp.mkdirs();
			System.out.println("ne");
		}
		File old = new File(storePath + "TaskOrganizer\\data\\tasks.tsk");
		try {
			fos = new ObjectOutputStream(new FileOutputStream(storePath
					+ "TaskOrganizer\\data\\tasks.tmp.tsk", true));
			fos.writeObject(taskList);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		old.delete();
		old = new File(storePath + "TaskOrganizer\\data\\tasks.tmp.tsk");
		old.renameTo(new File(storePath + "TaskOrganizer\\data\\tasks.tsk"));
	}

	public void saveTaskList(File file) {
		ObjectOutputStream fos;
		try {
			fos = new ObjectOutputStream(new FileOutputStream(
					file.getAbsolutePath(), true));
			fos.writeObject(taskList);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Task> getTaskList() {
		return taskList;
	}

	public static boolean isDevelop() {
		return develop;
	}

	public int getUnesolvedCount() {
		int n = 0;
		for (Task task : taskList) {
			if (!task.isResolved())
				n++;
		}
		return n;
	}

	public static void setDevelop(boolean develop) {
		AppCore.develop = develop;
	}

	public String getStorePath() {
		return storePath;
	}

}
